package com.benhil.hrms.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.benhil.hrms.model.Account;
import com.benhil.hrms.service.IAccountService;
import com.benhil.hrms.util.JsonResponseContainer;
import com.benhil.hrms.util.JsonResponseContainer.Response;

@Controller
@RequestMapping("account")
public class AccountController extends AbstractCrudController<Account>{

	@Autowired
	private IAccountService mService;

	@Override
	protected Response getCreateResponse(Account account) {
		JsonResponseContainer.Response response;
		
		try{
			
			List<Account> accounts = mService.create(account);
			response  = JsonResponseContainer.createSuccessResponse(accounts);

		} catch (Exception e) {
			response = JsonResponseContainer.createErrorResponse(e.getMessage());
		}
		
		return response;
	}

	@Override
	protected Response getReadResponse(int page, int start, int limit) {
		JsonResponseContainer.Response response;
		try{

			List<Account> accounts = mService.getAccountList();			
			response  = JsonResponseContainer.createSuccessResponse(accounts);

		} catch (Exception e) {
			response = JsonResponseContainer.createErrorResponse(e.getMessage());
		}
		
		return response;
	}

	@Override
	protected Response getUpdateResponse(Account account) {
		JsonResponseContainer.Response response;
		
		try{			
			List<Account> accounts = mService.update(account);
			response  = JsonResponseContainer.createSuccessResponse(accounts);

		} catch (Exception e) {
			response = JsonResponseContainer.createErrorResponse(e.getMessage());
		}
		
		return response;
	}

	@Override
	protected Response getDeleteResponse(Account account) {
		JsonResponseContainer.Response response;
		
		try{

			mService.delete(account);
			response  = JsonResponseContainer.createSuccessResponse("Data has been deleted");

		} catch (Exception e) {
			response = JsonResponseContainer.createErrorResponse(e.getMessage());
		}
		
		return response;
	}	
}
